//
//  SequencingView.m
//  phmall
//
//  Created by admin on 2017/10/19.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "SequencingView.h"

@interface SequencingView ()<UITableViewDataSource,UITableViewDelegate>{
    BOOL hide;
}
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *hideArray;
@end
@implementation SequencingView

-(instancetype)init{
    if ([super init]) {
        self.backgroundColor=[UIColor clearColor];
        if (@available(iOS 11.0, *)){
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);//导航栏如果使用系统原生半透明的，top设置为64
            self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
        }
       
        UIView *lucency=[[UIView alloc]init];
        lucency.backgroundColor=[UIColor blackColor];
        lucency.alpha=0.5;
        [self addSubview:lucency];
        [lucency mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
        }];
        
        UITapGestureRecognizer *lucencyTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lucencyAction:)];
        
        [self addGestureRecognizer:lucencyTap];
        
        UIView *backView=[[UIView alloc]init];
        [self addSubview:backView];
        backView.backgroundColor=[UIColor whiteColor];
        [backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.width.offset(167);
            make.bottom.equalTo(self).with.offset(0);
        }];
        
        [backView addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(backView).offset(20);
            make.left.right.bottom.equalTo(backView);
        }];
    }
    return self;
}
-(void)lucencyAction:(UITapGestureRecognizer *)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(withdrawSequencingView:)]){
        [self.delegate withdrawSequencingView:nil];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.hideArray[indexPath.section] isEqualToString:@"0"]) {
         return 0;
    }else{
        return 40;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 167, 44)];
    UILabel *title=[[UILabel alloc]init];
    [view addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view);
        make.left.equalTo(view).offset(20);
        make.top.bottom.equalTo(view);
    }];
    title.text=@"默认排序";
    title.font=[UIFont systemFontOfSize:13];
    
    view.tag=200+section;
    UITapGestureRecognizer *viewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapAction:)];
    [view addGestureRecognizer:viewTap];
    
    UIView *line=[[UIView alloc]init];
    [view addSubview:line];
    line.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).offset(20);
        make.right.equalTo(view).offset(-20);
        make.height.offset(1);
        make.top.equalTo(view);
    }];
    return view;
}
-(void)viewTapAction:(UITapGestureRecognizer *)tap{
    UIView *view = (UIView*) tap.view;
    if ([self.hideArray[view.tag-200] isEqualToString:@"0"]) {
        self.hideArray[view.tag-200]=@"1";
    }else{
        self.hideArray[view.tag-200]=@"0";
    }
    [self.tableView reloadData];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"SetCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;;
    }
    [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(40);
        make.right.equalTo(cell.contentView);
        make.top.bottom.equalTo(cell.contentView);
    }];
    cell.textLabel.text =@"从高到低";
    cell.textLabel.font=[UIFont systemFontOfSize:13];
    
    UIView *line=[[UIView alloc]init];
    [cell.contentView addSubview:line];
    line.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(40);
        make.right.equalTo(cell.contentView).offset(-20);
        make.height.offset(1);
        make.top.equalTo(cell.contentView);
    }];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.delegate && [self.delegate respondsToSelector:@selector(withdrawSequencingView:)]){
        [self.delegate withdrawSequencingView:nil];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //分割线补全
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

//MARK:-GETTER
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor=[UIColor whiteColor ];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.scrollEnabled = YES;
    }
    return _tableView;
}
-(NSMutableArray *)hideArray{
    if (!_hideArray) {
        _hideArray=[[NSMutableArray alloc]init];
        for (int i=0; i<5; i++) {
            [_hideArray addObject:@"0"];
        }
    }
    return _hideArray;
}
@end
